// pages/orderSuccess/orderSuccess.js
Page({
    data: {
      orderId: '',         // 订单ID
      orderInfo: {},       // 订单信息
      isLoading: true,     // 加载状态
      hasError: false,     // 错误状态
      errorMsg: '',         // 错误信息
      statusBarHeight:0
    },
  
    onLoad(options) {
        const sysInfo = wx.getSystemInfoSync()
        this.setData({
            statusBarHeight: sysInfo.statusBarHeight,
        })
      // 从参数获取订单ID
      const orderId = options.orderId;
      if (!orderId) {
        this.setData({
          hasError: true,
          errorMsg: '订单ID缺失',
          isLoading: false
        });
        return;
      }
      
      this.setData({
        orderId: orderId
      });
      
      // 查询订单信息
      this.queryOrderInfo(orderId);
    },
  
    // 查询订单信息
    async queryOrderInfo(orderId) {
      try {
        this.setData({
          isLoading: true
        });
        
        // 调用云函数查询订单
        const res = await wx.cloud.callFunction({
          name: 'queryOrder',
          data: {
            // orderId: '0253ee3668590ab600a7a07726661c1c'
            orderId:orderId
          }
        });
        console.log(res);
        if (res.result.success) {
          this.setData({
            orderInfo: res.result.orderData,
            isLoading: false
          });
        } else {
          this.setData({
            hasError: true,
            errorMsg: res.result.message || '查询订单失败',
            isLoading: false
          });
        }
      } catch (error) {
        console.error('查询订单异常:', error);
        this.setData({
          hasError: true,
          errorMsg: '系统错误，请重试',
          isLoading: false
        });
      }
    },
  
    // 返回首页
    goBackToHome() {
      wx.switchTab({
        url: '/pages/index/index'
      });
    },
  
    // 查看订单详情
    goToOrderDetail() {
      const { orderId } = this.data;
      if (orderId) {
        wx.navigateTo({
          url: `/pages/orderDetail/orderDetail?orderId=${orderId}`
        });
      }
    },
  });